(function() {
	var remote_server_url = "http://121.40.18.23:1518/api/complex/";

	var map; // 绑定map全局对象
	var localCity; // 绑定全局定位城市

	require(["baiduMap"], function(baiduMap) {
		baiduMap(function init_map() {
			map = new BMap.Map("store_map");
			console.log("map")
			map.addControl(new BMap.NavigationControl({
				anchor: BMAP_ANCHOR_TOP_LEFT,
				type: BMAP_NAVIGATION_CONTROL_SMALL
			})); //添加地图缩放控件	

			// map.addTileLayer(new BMap.PanoramaCoverageLayer());
			var stCtrl = new BMap.PanoramaControl(); //构造全景控件
			stCtrl.setOffset(new BMap.Size(20, 20));
			map.addControl(stCtrl); //添加全景控件

			map.addControl(new BMap.ScaleControl()); // 添加比例尺控件
			window.map = map;

			function _init_city() {
				return new Promise(function(resolve, reject) {
					(new BMap.LocalCity()).get(function(local_current_city) {
						localCity = local_current_city.name;
						map.centerAndZoom(localCity, 12);
						resolve(localCity);
					});
				});
			};
			_init_city().then(function(_current_city) {
				get_poi_by_page_index(0, _current_city);
			});
			// 获取指定页面标注点
			var get_poi_by_page_index = function(page_index, city) {
				App.set("$Cache.store_map.current_page", page_index);
				$.getJSON(remote_server_url + "ClientStoreMapFactory", {
					city: city,
					page_index: page_index || 0
				}, function(result) {
					console.log(result);
					App.set("$Cache.store_map.list", result);
					App.set("$Cache.store_map.page_size", Array(~~(result.total / 10) + 1));
					map_add_point(result.contents);
				});
			};
			App.set("$Event.store_map.get_poi_by_page_index", function(e, vm) {
				get_poi_by_page_index(vm.get("$Index"), localCity);
			});
			// 为地图添加带有信息框的标注点
			var map_add_point = function(marker_points) {
				map.getOverlays().forEach(function(item, index) {
					map.removeOverlay(item);
				});
				marker_points.forEach(function(point, index) {
					// 创建标注点
					var marker = new BMap.Marker(new BMap.Point(point.location[0], point.location[1]));
					// 设置信息框内容
					var sContent = "<a href='http://" + document.domain + "/store_preview.html?geotable_id=" + point.uid + "'>" +
						"<span style='font-size:16;font-weight:bold;padding-right:15px'>" + point.title + "</span>" +
						"<span style='color:blue'>详情...</span>" +
						"<div><img class='store_img' src='" + point.store_imgs.split(",")[0] + "?imageView/1/w/200' /></div>" +
						"</a>" +
						"<p>地址：" + point.address + "</p>" +
						"<p>电话：<a href='tel:" + (point.phone || point.mobile) + "'>" + (point.phone || point.mobile) + "</a></p>"
					map.addOverlay(marker);
					marker.addEventListener("click", function() {
						var info_window = marker_open_info_window(sContent);
						this.openInfoWindow(info_window);
					});
				});
			};
			// 标注点打开信息框事件
			var marker_open_info_window = function(content) {
				// 创建信息框对象并返回
				var infoWindow = new BMap.InfoWindow(content, {
					width: 200
				});
				// 信息框打开时重绘一次，确保内容煤油溢出或挤压
				infoWindow.addEventListener("open", function() {
					document.getElementsByClassName("store_img")[0].onload = function() {
						infoWindow.redraw();
						infoWindow.enableAutoPan();
					};
				});
				return infoWindow;
			};
		});
	});
	//百度地图
	// var ak_key = "rroTv2TmcdKGCiQ2jER1oA3V";
	// var doc_write = document.write;
	// var doc_s_write = function(HTML) {
	// 	var dom = $(HTML);
	// 	console.log("html" + HTML);
	// 	dom.find("script[src]").each(function(i, scriptNode) {
	// 		scriptNode.src && require([scriptNode.src]);
	// 		scriptNode.removeAttribute("src")
	// 	});
	// 	dom.appendTo(document.body);
	// }
	// document.write = doc_s_write;
	// require(["http://api.map.baidu.com/api?v=2.0&ak=Noga0lSLMrk62roymqXkgaUB"], function(scriptText) {
	// 	document.write = doc_write;
	// 	var _BMap_ti = setInterval(function() {
	// 		if (window.BMap) {
	// 			init_map();
	// 			clearInterval(_BMap_ti);
	// 		}
	// 	})
	// });

	// function init_map() {
	// 	return new Promise(function(resolve, reject) {
	// 		// 百度地图API功能
	// 		map = new BMap.Map("store_map"); // 创建Map实例
	// 		
	// 		map.setCenter("深圳")
	// 	});
	// };

}());